<script setup name="ArticleManage">
import ChannelSelect from './components/ChannelSelect.vue'
import ArticleEdit from './components/ArticleEdit.vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
import { articleGetListService, articleDelArticleService } from '@/api/article'
import { formatTime } from '@/utils/format'
const articleList = ref([
  {
    id: 5961,
    title: '新的文章啊',
    pub_date: '2022-07-10 14:53:52.604',
    state: '已发布',
    cate_name: '体育',
  },
])
const total = ref(0)
// 条件
let params = ref({
  pagenum: 1,
  pagesize: 2,
  cate_id: '',
  state: '',
})
const loading = ref(false)

/**
 * 获取文章列表
 */
const getArticleList = async () => {
  loading.value = true
  const res = await articleGetListService(params.value)
  articleList.value = res.data
  total.value = res.total
  loading.value = false
  console.log(total.value)
}
getArticleList()
/**
 * 每页条数改变
 * @param pagesize 每页条数
 */
const onSizeChange = (pagesize) => {
  params.value.pagenum = 1
  params.value.pagesize = pagesize
  getArticleList()
}
/**
 * 页码改变
 * @param pagenum 页码
 */
const onCurrentChange = (pagenum) => {
  params.value.pagenum = pagenum
  getArticleList()
}
/**
 * 搜索
 */
const onSearch = () => {
  params.value.pagenum = 1
  getArticleList()
}
/**
 * 重置
 */
const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}
// 文章编辑组件实例
const articleEditRef = ref()
/**
 * 发布文章
 */
const onPublishArticle = () => {
  articleEditRef.value.open({})
}
/**
 * 重新渲染
 * @param type 发布 | 编辑
 */
const onSuccess = (type) => {
  if (type === 'add') {
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }

  getArticleList()
}
/**
 * 编辑
 * @param row 当前行数据
 */
const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}
/**
 * 删除
 * @param row 当前行数据
 */
const onDelArticle = async (row) => {
  await ElMessageBox.confirm('您确定要删除该文章么', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确定',
    cancelButtonText: '取消',
  })
  await articleDelArticleService(row.id)
  ElMessage.success('删除成功~')
  getArticleList()
}
</script>

<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onPublishArticle"> 发布文章</el-button>
    </template>
    <!-- 条件 -->
    <el-form inline>
      <el-form-item label="文章分类：">
        <channel-select v-model="params.cate_id" width="240px"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select size="large" style="width: 240px" v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 内容 -->
    <el-table :data="articleList" style="width: 100%" v-loading="loading">
      <el-table-column label="文章标题">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button
            circle
            plain
            type="primary"
            :icon="Edit"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            circle
            plain
            type="danger"
            :icon="Delete"
            @click="onDelArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[1, 2, 3, 4]"
      :background="true"
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />

    <!-- 编辑 -->
    <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
  </page-container>
</template>

<style scoped></style>
